package org.water.config;

import java.util.List;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.water.common.bind.method.CurrentUserMethodArgumentResolver;
import org.water.common.bind.method.ResultFormatReturnValueHandler;
import org.water.common.interceptors.CommonInterceptor;

/**
 * web配置
 * @author qzy
 *
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
//    @Override
//    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
//
//        //生成json时，将所有Long转换成String
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        objectMapper.registerModule(simpleModule);
//
//        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        converters.add(0, jackson2HttpMessageConverter);
//    }
	
	
	/**
	 * 允许访问的域 
	 */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
            .addMapping("/**")
            .allowedOrigins("*")
            .allowCredentials(true)
            .allowedHeaders("*")
            .allowedMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS");
            //.maxAge(3600);
      //  WebMvcConfigurer.super.addCorsMappings(registry);
    }
    
	@Override
	public void addViewControllers(ViewControllerRegistry viewcontrollerregistry) {
		
		//WebMvcConfigurer.super.addViewControllers(viewcontrollerregistry);
	}
	/**
	 * 方法参数处理
	 */
	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> list) {
		list.add(new CurrentUserMethodArgumentResolver());
		//WebMvcConfigurer.super.addArgumentResolvers(list);
	}
	
	
	/**
	 * 返回数据统一处理
	 */
	@Override
	public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> list) {
		list.add(0, new ResultFormatReturnValueHandler());
		//WebMvcConfigurer.super.addReturnValueHandlers(list);
	}
	/**
	 * 增加拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry interceptorregistry) {
		interceptorregistry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
		//WebMvcConfigurer.super.addInterceptors(interceptorregistry);
	}
	/**
	 * 配置静态资源访问的路径规则以及查找静态资源的路径
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    /**
	     * 处理所有HTML的请求，到static目录下查找对应的资源
	     */
	    registry.addResourceHandler("/**.html","/static/*") //处理的路径规则 
	            .addResourceLocations("classpath:/static/")  //到哪些目录下去查找静态资源    
	    ;
	    
	    //WebMvcConfigurer.super.addResourceHandlers(registry);
	}
	/**
	 * 路径配置处理
	 */
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		configurer.setUseTrailingSlashMatch(true)   //地址最后/识别，加不加都可以
		          .setUseSuffixPatternMatch(true);  //地址后加后缀
		//WebMvcConfigurer.super.configurePathMatch(configurer);
	}
	
	/**
	 * web容器配置
	 * @return
	 */
	@Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
			@Override
            public void customize(ConfigurableWebServerFactory factory) {
				//增加错误页面
				factory.addErrorPages(new ErrorPage("/error/default"),new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"),new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
            }
        };
    }
}
